import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../generated/assets.dart';
import 'logic.dart';

class SendCodePage extends StatefulWidget {
  @override
  _SendCodePageState createState() => _SendCodePageState();
}

class _SendCodePageState extends State<SendCodePage> {

  TextEditingController _codeController = TextEditingController();
  final logic = Get.put(SendCodeLogic());



  Timer? _smsTimer;
  Timer? _voiceTimer;
  int _smsCountdown = 0;
  int _voiceCountdown = 0;
  String _smsFetch = '重新获取';
  int _voiceFetchCount = 2;
  bool _isCanClick = false ;
  @override
  void initState() {
    // TODO: implement initState
    logic.registerCallback(_obtianTra);
  }


  void _startSMSTimer() {
    _isCanClick = true;
    setState(() {
      _smsCountdown = 60;
    });
    logic.isObtainYanZ.value = true;
    logic.choosetype = messType.msm;
    _smsTimer?.cancel();
    _smsTimer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (_smsCountdown == 0) {
        timer.cancel();
        logic.choosetype = messType.onoe;
        _isCanClick = false;
      } else {
        setState(() {
          _smsCountdown--;
        });
      }
    });
  }

  Widget _SMSTExt ( messType type)  {
    if (_smsCountdown > 0) { //验证码倒计时
      switch (type) {
        case messType.msm:
          return  Text("${_smsCountdown}s",style:TextStyle(color: Color(0xFF999999)),);
        case messType.voice:
          if (logic.isObtainYuYo.value) {
            return Text("重新获取",style:TextStyle(color: Color(0xFF999999)),);
          }
          return  Text("获取语音验证码",style:TextStyle(color: Color(0xFF999999)),);
        case messType.onoe:
          return  Text("");
      }
      return Text("${_smsCountdown}s",style:TextStyle(color: Color(0xFF999999)),);
    }else if  (_voiceCountdown > 0) {//验证码倒计时
      switch (type) {
        case messType.msm:
          if (logic.isObtainYanZ.value) {
            return Text("重新获取",style:TextStyle(color: Color(0xFF999999)),);
          }
          return  Text("获取验证码",style:TextStyle(color: Color(0xFF999999)),);
        case messType.voice:
          return  Text("${_smsCountdown}s",style:TextStyle(color: Color(0xFF999999)),);
        case messType.onoe:
          return  Text("");
      }
    }else  {//都没获取中
      switch (type) {
        case messType.msm:
          if (logic.isObtainYanZ.value) {
            return Text("重新获取",style:TextStyle(color: Color(0xFFD43D3F )),);
          }
          return  Text("获取验证码",style:TextStyle(color: Color(0xFFD43D3F )),);
        case messType.voice:
          if (logic.isObtainYuYo.value) {
            return Text("重新获取",style:TextStyle(color: Color(0xFFD43D3F )),);
          }
          return  Text("获取语音验证码",style:TextStyle(color: Color(0xFFD43D3F )),);
        case messType.onoe:
          return  Text("");
      }
    }
  }
  void _obtianTra(){
    if (logic.choosetype == messType.msm){
      _startVoiceTimer();
    }
    if (logic.choosetype == messType.voice){
      _startSMSTimer();
    }
  }

  void _startVoiceTimer() {

    _isCanClick = true;
    setState(() {
      _voiceCountdown = 60;
    });
    logic.isObtainYuYo.value = true;
    logic.choosetype = messType.voice;
    _voiceTimer?.cancel();
    _voiceTimer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (_voiceCountdown == 0) {
        timer.cancel();
        _isCanClick = false;
        logic.choosetype = messType.onoe;
      } else {
        setState(() {
          _voiceCountdown--;
        });
      }
    });
  }

  @override
  void dispose() {
    _smsTimer?.cancel();
    _voiceTimer?.cancel();
    _codeController.dispose();
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: BackButton(color: Colors.black),
        backgroundColor: Colors.white,
        elevation: 0,
        actions: [
          TextButton(
            onPressed: () {
              // TODO: 跳转其他账号登录逻辑
            logic.backTO();
            },
            child: Text(
              "用其他账号登录",
              style: TextStyle(color: Color(0xFFD43D3F),
                  fontSize: 14,
                  fontWeight: FontWeight.w600),
            ),
          ),
        ],
      ),
      backgroundColor: Colors.white,
      body: GestureDetector(
        onTap: (){
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Column(
          children: [
            Container(
              height: 400,
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 30),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SizedBox(height: 30),
                    Text(
                      "验证码登录",
                      style: TextStyle(fontSize: 28, fontWeight: FontWeight.bold),
                    ),
                    SizedBox(height: 63),
                    Container(
                      decoration: BoxDecoration(
                        border: Border.all(color: Colors.red),
                        borderRadius: BorderRadius.circular(30),
                      ),
                      padding: EdgeInsets.symmetric(horizontal: 20),
                      child: Row(
                        children: [
                          Expanded(
                            child: TextField(
                              controller: _codeController,
                              decoration: InputDecoration(
                                hintText: "请输入验证码",
                                border: InputBorder.none,
                              ),
                              onChanged: (text) {
                                // 处理文本变化事件

                                logic.telephoneNUm = text ;
                              },
                              keyboardType: TextInputType.number,
                            ),
                          ),
                          AbsorbPointer(
                            absorbing: _isCanClick,
                            child:
                            TextButton(
                              onPressed: _smsCountdown == 0 ? () {
                                logic.choosetype = messType.msm;
                                logic.gotoWeb();
                                // TODO: 请求短信验证码接口
                              } : null,
                              child:  _SMSTExt(messType.msm),
                            ),
                          ),
                        ],
                      ),
                    ),
                    SizedBox(height: 8),
                    Visibility(
                      visible: _smsCountdown > 0 ? false : true ,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            "短信验证码已发送至您的手机，请注意查收",
                            style: TextStyle(color: Color(0xFF999999)),
                          )
                        ],
                      ),),

                    SizedBox(height: 8),
                    Center(
                      child: SizedBox(
                        width: double.infinity,
                        height: 50,
                        child: ElevatedButton(
                          style: ElevatedButton.styleFrom(
                            backgroundColor: Colors.red,
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(30),
                            ),
                          ),
                          onPressed: () {
                            logic.clickGotoNext();
                          },
                          child: Text(
                            "下一步",
                            style: TextStyle(fontSize: 16, color: Colors.white),
                          ),
                        ),
                      ),
                    ),
                    SizedBox(height: 0, child:
                    Container(
                      color: Colors.red,
                    ),),
                    Center(

                      child: Container(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text("收不到短信验证码？",style: TextStyle(color: Color(0xFF000000)),),
                            TextButton(
                              onPressed: _voiceCountdown == 0 ? () {
                                logic.choosetype = messType.voice;
                                logic.gotoWeb();
                                // TODO: 获取语音验证码接口
                              } : null,
                              child: _SMSTExt(messType.voice),
                            ),
                          ],
                        ),
                      ),
                    ),
                    Spacer(),
                    SizedBox(height: 10),
                  ],
                ),
              ),
            ),
            Spacer(),
            Container(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Obx(() {
                    return InkWell(
                      child: Container(
                        width: 17,
                        height: 17,
                        child: Image.asset(logic.isselect.value
                            ? Assets.imagesIcRadioB
                            : Assets.imagesIcRadioA),
                      ),
                      onTap: () {
                        logic.clickSelect();
                      },
                    );
                  }),
                  SizedBox(width: 5),
                  Text(
                    "同意与阅读",
                    style: TextStyle(color: Colors.grey),
                  ),
                  InkWell(
                    child: Text(
                      "《隐私政策》",
                      style: TextStyle(color: Colors.red),
                    ),
                    onTap: () {

                    },
                  ),
                  Text(
                    "和",
                    style: TextStyle(color: Colors.grey),
                  ),
                  InkWell(
                    child: Text(
                      "《用户服务协议》",
                      style: TextStyle(color: Colors.red),
                    ),
                    onTap: () {

                    },
                  ),
                ],
              ),
            ),
            SizedBox(height: 30),
          ],
        ),
      ),
    );
  }
}


